package com.sp.server.domain;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

@RooJavaBean
@RooToString
@RooJpaActiveRecord
public class MailMessage {

	
	final public static int T0_HOSTS=0;
	final public static int TO_CLIENTS=1;
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
	private Long id;
	
	@Version
    @Column(name = "version")
    private Integer version;
	
	private int destination;
	
	private String topic;
	
	private String address;
	
	private String from;
	
	private String message;
	@Temporal(TemporalType.DATE)
	private Date date;
	
	@Transient
	private String fromInfo="администрация vpiter-vip";
	
	
	public void persist(){
		
	}
	
	public void send(){
		
//		String mailHost = "smtp.realgroup.su";
		String mailHost = "smtp.gmail.com";
//		final String address = mailMessage.getAddress();
//	    final String from = mailMessage.getFrom();
	    final String replyTo = getFrom();
	    // Get system properties
	    
	    
	    
	    Properties props = new Properties();

	    // Setup mail server
	    
	    props.put("mail.smtp.host", mailHost);
	    props.put("mail.smtp.auth", "true");
	    props.put("mail.smtp.starttls.enable","true");
	    props.put("mail.smtp.starttls.required","true");
	    props.put("mail.smtp.port", "587");
	    
	    // Get session
	    final Session session = 
	      Session.getInstance(props, new Authenticator(){
	    		protected PasswordAuthentication getPasswordAuthentication(){
	    			return(new PasswordAuthentication("mail@vpiter-vip.ru","newindia12"));
	    		}
	      });
	    
	   
	    
//	    final Session session = 
//	    	      Session.getInstance(props, new Authenticator(){
//	    	    		protected PasswordAuthentication getPasswordAuthentication(){
//	    	    			return(new PasswordAuthentication("m20091061_1","gWhthaxC"));
//	    	    		}
//	    	      });

	    // Define message
	    try {
			MimeMessage message = new MimeMessage(session);
			message.setHeader("Content-type", "text/html; charset=UTF-8");
			message.setFrom(new InternetAddress(from,fromInfo,"UTF-8"));
			
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(address));
			
			String subject=topic==null?"vpiter-vip: бронирование":topic;
			message.setSubject(subject,"UTF-8");
			message.setReplyTo(new InternetAddress[]{new InternetAddress(replyTo)});
		
			message.setText(getMessage(),"UTF-8");
			// create the message part 
//			MimeBodyPart messageBodyPart = new MimeBodyPart();
//
//			//fill message
//			messageBodyPart.setText(getMessage());
//			
//			Multipart multipart = new MimeMultipart();
//			multipart.addBodyPart(messageBodyPart);
//			
//			message.setContent(multipart);

			// Send the message
			
//			try {
//				message.writeTo(System.out);
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			
			
			Transport.send( message );
			
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		
		
	}
	
	

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getDestination() {
		return destination;
	}

	public void setTo(int destination) {
		this.destination = destination;
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}



	public String getAddress() {
		return address;
	}



	public void setAddress(String address) {
		this.address = address;
	}



	public String getFrom() {
		return from;
	}



	public void setFrom(String from) {
		this.from = from;
	}

	

	public String getFromInfo() {
		return fromInfo;
	}

	public void setFromInfo(String fromInfo) {
		this.fromInfo = fromInfo;
	}
	
	


}
